#pragma once
#include <map>
#include <functional>
#include "Creature.h"
#include "Player.h"
#include "base/core/singleton.h"
#include "Common/CharacterDefine.h"
#include "base/core/object_pool.h"
#include "base/core/klist.h"
#include "CreatureMgr.h"
#include "MovePart.h"
#include "SkillPart.h"
#include "PackagePart.h"
#include "MissionPart.h"
#include "ChatPart.h"
#include "DuplicatePart.h"
#include "DeputyPart.h"
#include "Character/BuffPart.h"
#include "AchievementPart.h"
#include "TreasurePart.h"
#include "CollectPart.h"
#include "Character/PvpPart.h"

#include "Character/CreatureMgr.h"

namespace
{
	static const uint32_t s_leaveSaveCountPerFrame = 50;    // 退出游戏存盘每帧频率
	static const uint32_t s_leaveSaveInterval = 30;
}

class PlayerMgr : public IDBProxyMessageHandler,
				  public ManualSingleton<PlayerMgr>,
				  public IServerMessageHandler,
				  public IClientMessageHandler
{
	friend class ManualSingleton<PlayerMgr>;
	//typedef std::unordered_map<CharIDType, Player*>			PlayerObjMap;
	typedef std::unordered_map<CharIDType, uint64_t>		DISCONNECT_MAP;
	typedef std::unordered_map<CharIDType, uint64_t>		DELPLAYER_MAP;
	typedef std::unordered_map<CharIDType, CharLoginInfo>   CHAR_LOGIN_MAP;
protected:
	//PlayerObjMap m_mapPlayer;				// 玩家数据表
	//KList m_onlineList;						// 正常在线玩家列表
	//KList m_leaveList;						// 要离开的玩家列表，处理玩家数据存盘时用

	SET_UINT64    m_reqDBcid;   //暂时加个cid请求db数据的缓冲列表，以后DB改了这里再删除
	MAP_UINT64_UINT32 m_reqDB;
	uint32_t m_req;
	DISCONNECT_MAP    m_disconnectMap;         // 断线重连cid列表
	DELPLAYER_MAP     m_delPlayerMap;          //删除玩家角色数据
	CHAR_LOGIN_MAP	  m_loginMap;			//玩家登录信息

protected:
	//from gate 
	bool EnterLogicReq(RECV_PACKAGE& package);
	bool LeaveLogicReq(RECV_PACKAGE& package);
	bool OnRankingList(RECV_PACKAGE& package);
	void OnMsgHandle(RECV_PACKAGE& package);

	//数据角色db回复
	bool CharDBRet(RECV_PACKAGE& package);
	bool EnterGameRspToCenter(Player *pPlayer);

	uint32_t NewReq();
	CharLoginInfo *GetCharLogin(CharIDType cid);
	void DelCharLogin(CharIDType cid);

public:
	///////////////////////////////  IDBProxyMessageHandler ///////////////////////////////////////////
	virtual void		onDBProxyMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getDBProxyHandlerDebugInfo(void) { return "PlayerMgr::onDBProxyMessage"; };

	virtual void		onServerMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getServerHandlerDebugInfo(void) { return "PlayerMgr::onserverMessage"; };

	virtual void		onClientMessage(uint32_t actionId, RECV_PACKAGE& package, uint64_t Id = 0);
	virtual const char* getClientHandlerDebugInfo(void) { return "PlayerMgr::onclientMessage"; };

public:
	bool Init();
	void Update(uint64_t tick);
	bool UnInit();
	void OnDayChange();
	bool LeaveGame(CharIDType cid, uint32_t uid, uint32_t clientId, LOGOUT_TYPE type);
	bool EnterGame(CharIDType cid, CharLoginInfo& loginInfo,bool changeLogic = false);
	void L2C_AddPrestige(MAP_UINT64_INT32 &mapAttr);
	PlayerMgr();
	virtual ~PlayerMgr();
};

#define g_MakePlayerMgr() (PlayerMgr::Instance())
#define g_GetPlayerMgr()  (PlayerMgr::GetInstance())
#define g_DelPlayerMgr()  (PlayerMgr::Destroy())
